package pacman;

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*; 
import java.util.Random;

public class AnimatedSprite extends Sprite implements Runnable{

	public int AnimationSleepTime;
	public int Orientation;
	private final int OR_UP=1, OR_DOWN=2,OR_LEFT=4,OR_RIGHT=3;
	public int deltaX,deltaY;
	private int nextMove=0;
	private Random randomGenerator;
	
	
	public AnimatedSprite(Image image,int frameWidth,int frameHeight,int AnimationSleepTime)
	{
		super(image,frameWidth,frameHeight);
		randomGenerator = new Random();
		this.AnimationSleepTime=AnimationSleepTime;
		this.setFrame(0);
		
	}

	public void run(){
		while(2>1)
		{
			if(this.getFrame()==this.getFrameSequenceLength()-1)
			{
				this.setFrame(0);
			}
			else
			{
				this.setFrame(this.getFrame()+1);
			}
			try { 
				Thread.sleep(AnimationSleepTime); 
			} catch (InterruptedException e) { 
				System.out.println(e.toString()); 
			}
		}
	}
	
	public void setOrientation(int newOrientation)
	{
		this.Orientation=newOrientation;
	}
	
	public int getOrientation()
	{
		return Orientation;
	}
	
	
	
	public void evaluateRandomMovement(int delta,Image image,boolean evaluator)
	{
		int random = Math.abs(randomGenerator.nextInt()%5);
		if(((random==1 && Orientation==2)||(random==2 && Orientation==1)||(random==4 && Orientation==3)||(random==3 && Orientation==4))==false)
		{
			this.setNextMove(random);
			this.switchNextMove(delta, image, evaluator);
		}
	}

	
	
	
	
	
	
	/*
	public void evaluateRandomMovement(int delta,Image image,boolean evaluator)
	{
		int random = Math.abs(randomGenerator.nextInt()%5);
		this.setNextMove(random);
		this.switchNextMove(delta, image, evaluator);
	}
	*/
	
	
	public void doMovement(Canvas canvas)
	{
		if (this.getX()==(canvas.getWidth())-5)
		{
			this.setPosition(-5, this.getY());
		}
		else if (this.getX()==-5){
			this.setPosition(canvas.getWidth()-5, this.getY());
		}
		if (this.getX()+this.getDeltaX()>=-5 && (this.getX()+this.getDeltaY())<canvas.getWidth()+5 && this.getY()+this.getDeltaY()>=-5 && this.getY()+this.getDeltaY()<canvas.getHeight()+5)
		{ 
			this.setPosition(this.getX()+this.getDeltaX(), this.getY()+this.getDeltaY());
		}	
	}
	
	
	
	
	/*
	public void doMovement(boolean isRandom,Canvas canvas)
	{
		if (this.getX()+this.getDeltaX()>=-5 && (this.getX()+this.getDeltaY())<canvas.getWidth()+5 && this.getY()+this.getDeltaY()>=-5 && this.getY()+this.getDeltaY()<canvas.getHeight()+5)
		{ 
			this.setPosition(this.getX()+this.getDeltaX(), this.getY()+this.getDeltaY());
		}
		if (this.getX()==(canvas.getWidth()))
		{
			this.setPosition(-5, this.getY());
		}
		else if (this.getX()==-5){
			this.setPosition(canvas.getWidth(), this.getY());
		}
	}
	*/
	public void evaluateCrash(Image imagen)
	{
		boolean crash=false;
		switch(Orientation)
		{
		case OR_UP:
			if(collidesWith(imagen,0,1,true)){
				crash=true;
			}
			break;
		case OR_DOWN:
			if(collidesWith(imagen,0,-1,true)){
				crash=true;
			}
			break;
		case OR_LEFT:
			if(collidesWith(imagen,1,0,true)){
				crash=true;
			}
			break;
		case OR_RIGHT:
			if(collidesWith(imagen,-1,0,true)){
				crash=true;
			}
			break;
		}
		if(crash==true){
			setDeltaX(0);
			setDeltaY(0);
		}
	}


	public void setDeltaX(int deltaX) {
		this.deltaX = deltaX;
	}
	public int getDeltaX() {
		return deltaX;
	}

	public void setDeltaY(int deltaY) {
		this.deltaY = deltaY;
	}

	public int getDeltaY() {
		return deltaY;
	}

	public void setNextMove(int nextMove) {
		this.nextMove = nextMove;
	}

	public int getNextMove() {
		return nextMove;
	}
	
	public void switchNextMove(int delta,Image image,boolean evaluator)
	{
		switch (this.nextMove){ 
		case OR_LEFT:
			if(this.collidesWith(image,0+4,0,true)==evaluator)
			{
				this.setDeltaX(-1*delta);
				this.setDeltaY(0);
				this.changePosition(OR_LEFT);
				this.nextMove=0;
			}
			break; 
		case OR_RIGHT: 
			if(this.collidesWith(image,0-4,0,true)==evaluator)
			{
				this.setDeltaX(delta);
				this.setDeltaY(0);
				this.changePosition(OR_RIGHT);
				this.nextMove=0;
			}
			break; 
		case OR_UP: 
			if(this.collidesWith(image,0,0+4,true)==evaluator)
			{
				this.setDeltaX(0);
				this.setDeltaY(-1*delta);
				this.changePosition(OR_UP);
				this.nextMove=0;
			}
			break; 
		case OR_DOWN:
			if(this.collidesWith(image,0,0-4,true)==evaluator)
			{
				this.setDeltaX(0);
				this.setDeltaY(delta);
				this.changePosition(OR_DOWN);
				this.nextMove = 0;
			}
			break; 
		}
	}
	
	public void changePosition(int newPosition)
	{
		switch (newPosition){
		case OR_LEFT:
			this.setTransform(Sprite.TRANS_MIRROR);
			this.setOrientation(OR_LEFT);
			break;
		case OR_RIGHT:
			this.setTransform(Sprite.TRANS_NONE);
			this.setOrientation(OR_RIGHT);
			break;
		case OR_UP:
			this.setTransform(Sprite.TRANS_ROT270);
			this.setOrientation(OR_UP);
			break;
		case OR_DOWN:
			this.setTransform(Sprite.TRANS_ROT90);
			this.setOrientation(OR_DOWN);
			break;
		}
	}
}
